package com.example.taobaounion3.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.taobaounion3.R;
import com.example.taobaounion3.model.domain.HomePagerContent;
import com.example.taobaounion3.utils.LogUtils;
import com.example.taobaounion3.utils.urlUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomePagerContentAdapter extends RecyclerView.Adapter<HomePagerContentAdapter.InnerHolder> {
    List<HomePagerContent.DataBean> mdata =new ArrayList<>();
    private OnListerItemClickListener mItemClickLister=null;

    @NonNull
    @Override
    public HomePagerContentAdapter.InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView=LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_pager_content,parent,false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull HomePagerContentAdapter.InnerHolder holder, int position) {
         HomePagerContent.DataBean dataBean= mdata.get(position);
        //设置数据
        holder.setData(dataBean);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mItemClickLister!=null) {
                    HomePagerContent.DataBean item=mdata.get(position);
                    mItemClickLister.onItemClick(item);
                }
            }
        });
    }

    @Override
    public int getItemCount() {

        return mdata.size();
    }

    public void setMdata(List<HomePagerContent.DataBean> contents) {
        mdata.clear();
        mdata.addAll(contents);
        notifyDataSetChanged();
    }

    public void addData(List<HomePagerContent.DataBean> contents) {
        //添加之前的原来的size
        int olderSize=mdata.size();
        mdata.addAll(contents);
        //更新UI
        notifyItemRangeChanged(olderSize,contents.size());
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.goods_cover)
        public ImageView cover;

        @BindView(R.id.goods_title)
        public TextView title;

        @BindView(R.id.goods_off_price)
        public TextView offPriceTv;

        @BindView(R.id.goods_after_off_price)
        public TextView finalPriceTv;

        @BindView(R.id.goods_original_price)
        public TextView originalPriceTv;

        @BindView(R.id.goods_sell_count)
        public TextView sellCount;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }

        public void setData(HomePagerContent.DataBean dataBean) {
            Context context=itemView.getContext();
            title.setText(dataBean.getTitle());
//            ViewGroup.LayoutParams layoutParams=cover.getLayoutParams();
//            int width =layoutParams.width;
//            int height=layoutParams.height;
//            int coverSize=(width>height?width:height)/2;
            String coverPath= urlUtils.getCoverPath(dataBean.getPict_url());
            //LogUtils.d(HomePagerContentAdapter.this,"coverPath-->"+coverPath);
//            LogUtils.d(HomePagerContentAdapter.this,"width-->"+width);
//            LogUtils.d(HomePagerContentAdapter.this,"height-->"+height);
            //LogUtils.d(this,"url-->"+dataBean.getPict_url());
            Glide.with(context).load(coverPath).into(cover);
            String finalPrice=dataBean.getZk_final_price();
            long couponAmout=dataBean.getCoupon_amount();
            float resultPrice=Float.parseFloat(finalPrice)-couponAmout;
            offPriceTv.setText(String.format(context.getString(R.string.text_goods_off_price),couponAmout));
            finalPriceTv.setText(String.format("%.2f",resultPrice));
            originalPriceTv.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            originalPriceTv.setText(String.format(context.getString(R.string.text_goods_original_price),finalPrice));
            sellCount.setText(String.format(context.getString(R.string.text_goods_sell_count),dataBean.getVolume()));

        }
    }

    public void setOnListerItemClickListener(OnListerItemClickListener listener){
        this.mItemClickLister=listener;
    }

    public interface OnListerItemClickListener{
        void onItemClick(HomePagerContent.DataBean item);
    }
}
